package Offer.offer2017;

import domain.ListNode;

/**
 * O(1)时间删除结点
 * Created by 雷文康 on 2017/12/30.
 */
public class Test13 {
    public static void deleteNode(ListNode head, ListNode deleted){
        if (head == null || deleted == null){
            return;
        }
        //若链表只有头结点
        if(head == deleted && head.next == null){
            head = null;
            return;
        }
        //删除结点为尾结点
        if (deleted.next == null){
             ListNode p = head;
             while (p.next != deleted) {
                 p = p.next;
             }
             p.next = null;
             return;
        }

        ListNode next = deleted.next;
        deleted.val = next.val;
        deleted.next = next.next;
    }
}
